package com.glasses.business.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.glasses.business.R;
import com.glasses.business.utils.Tools;

import net.neiquan.applibrary.wight.ProgressWebView;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by cxh on 2016/9/9.
 */
public class WebDetailActiviy extends Activity {
    @InjectView(R.id.ly_back)
    LinearLayout lyBack;
    @InjectView(R.id.title_tv)
    TextView titleTv;
    @InjectView(R.id.baseweb_webview)
    ProgressWebView mWebView;
    private String title;
    private String url;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);
        ButterKnife.inject(this);
        mWebView.getSettings().setJavaScriptEnabled(true);
        if (getIntent() != null) {
            Bundle bundle = getIntent().getExtras();
            title = bundle.getString("title");
            url = bundle.getString("url");
            titleTv.setText(title);
            if (!TextUtils.isEmpty(url)) {
                mWebView.loadUrl(url);
            } else {
                mWebView.loadUrl("http://www.loho88.com/?utm_source=bdss2&utm_medium=cpc&utm_campaign=1%2E12&utm_term=%E7%AB%9E%E7%A7%80%5F%5F%E6%A0%87%E9%A2%98");
            }
        } else {
            titleTv.setText("详情");
            mWebView.loadUrl("http://www.loho88.com/?utm_source=bdss2&utm_medium=cpc&utm_campaign=1%2E12&utm_term=%E7%AB%9E%E7%A7%80%5F%5F%E6%A0%87%E9%A2%98");
        }
//        WebSettings webSettings = webView.getSettings();
//        webSettings.setJavaScriptEnabled(true);
//        webSettings.setAllowFileAccess(true);
//        webSettings.setBuiltInZoomControls(false);
        Tools.showDialog(this);
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url.startsWith("http:") || url.startsWith("https:")) {
                    view.loadUrl(url);
                    return false;
                }
                // Otherwise allow the OS to handle things like tel, mailto, etc.
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity(intent);
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                Tools.dismissWaitDialog();
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }
        });
        mWebView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (event.getAction() == KeyEvent.ACTION_DOWN) {
                    if (keyCode == KeyEvent.KEYCODE_BACK) {
                        finish();
                        return true;
                    }
                }
                return false;
            }
        });
    }

    @OnClick(R.id.ly_back)
    public void onClick() {
        finish();
    }
}
